/**
 * @file lsm303d.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2022-12-20
 * 
 * @copyright Copyright (c) 2022
 * 
 */

/* Head file */
#include "lsm303d.h"
#include "lsm303d_spi.h"
#include "string.h"




#define LSM303D_ASSERT(expr) if(!(expr)) {while(1);}




int8_t LSM303D_PollingMagRawData( _3AxisMagDataStruct *raw_data)
{
    int8_t ret;
    uint8_t origin_data[6];
    LSM303D_ASSERT(raw_data != 0)

    ret = lsm303d_read_reg(LSM_OUT_X_L_M, origin_data, sizeof(origin_data));
    if(ret == 0){
        raw_data->MagData_X = (origin_data[1]<<8) + origin_data[0];
        raw_data->MagData_Y = (origin_data[3]<<8) + origin_data[2];
        raw_data->MagData_Z = (origin_data[5]<<8) + origin_data[4];
    }
    
    return ret;
}



int8_t LSM303D_PollingTempRawData( uint16_t *temp_data)
{
    int8_t ret;
    uint16_t origin_data;
    LSM303D_ASSERT(temp_data != 0)

    ret = lsm303d_read_reg(LSM_TEMP_OUT_L, (uint8_t *)&origin_data, sizeof(origin_data));
    if(ret == 0){
        *temp_data = origin_data;
    }

    return ret;
}




int8_t LSM303D_GetWhoAmI(uint8_t *who_am_i)
{
    int8_t ret;

    ret = lsm303d_read_reg(LSM_WHO_AM_I, who_am_i, sizeof(who_am_i));

    return ret;
}


int8_t LSM303D_SetRegMode(){
    uint8_t dat;
    dat = 0x67;
    while(lsm303d_write_reg(LSM_CTRL1,&dat,1) != 0);//持续模式，100HZ
    dat = 0xC8;
    while(lsm303d_write_reg(LSM_CTRL2,&dat,1) != 0);//50Hz带宽，+-4g
    dat = 0xF0;
	while(lsm303d_write_reg(LSM_CTRL5,&dat,1) != 0);//50Hz带宽
    dat = 0x00;
	while(lsm303d_write_reg(LSM_CTRL6,&dat,1) != 0);//+-2gauss
    dat = 0x80;
	while(lsm303d_write_reg(LSM_CTRL7,&dat,1) != 0);
    return 0;
}











